package cn.easybuy.daoImpl;

import cn.easybuy.dao.UserDao;
import cn.easybuy.entity.User;
import cn.easybuy.utils.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {


    @Override
    public int addUser(User user) throws SQLException{
        String sql="insert into easybuy_user (loginName,userName,`password`,sex,identityCode,email,mobile,type) value (?,?,?,?,?,?,?,?)";
        Object[] param={user.getLoginName(),user.getUserName(),user.getPassword(),user.getSex(),user.getIdentityCode(),user.getEmail(),user.getMobile(),user.getType()};
        return executeUpdate(sql,param);
    }

    @Override
    public int logoffUser(User user) throws SQLException{
        String sql="delete from easybuy_user where id=?";
        Object[] param={user.getId()};
        return executeUpdate(sql,param);
    }

    @Override
    public User loginUser(User user)throws SQLException {
        String sql="SELECT id,loginName,userName,`password`,sex,identityCode,email,mobile,type FROM easybuy_user where loginName=? and `password`=?";
        Connection conn=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        User use=null;
        try {
            conn=getConnection();
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,user.getLoginName());
            pstmt.setString(2,user.getPassword());
            rs=pstmt.executeQuery();
            if(rs.next()){
                int id=rs.getInt("id");//用户编号
                String loginName=rs.getString("loginName");//登录名
                String userName=rs.getString("userName");//用户名
                String password=rs.getString("password");//密码
                int sex=rs.getInt("sex");//性别
                String identityCode=rs.getString("identityCode");//身份证号
                String email=rs.getString("email");//邮箱
                String mobile=rs.getString("mobile");//手机号
                int type=rs.getInt("type");//类型（前后台）
                use=new User(id,loginName,userName,password,sex,identityCode,email,mobile,type);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return use;
    }

    @Override
    public List<User> listAll() throws SQLException{
        String sql="SELECT id,loginName,userName,`password`,sex,identityCode,email,mobile,type FROM easybuy_user ";
        Connection conn=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        List<User> users=new ArrayList<User>();
        try {
            conn=getConnection();
            pstmt=conn.prepareStatement(sql);
            rs=pstmt.executeQuery();
            while(rs.next()){
                int id=rs.getInt("id");//用户编号
                String loginName=rs.getString("loginName");//登录名
                String userName=rs.getString("userName");//用户名
                String password=rs.getString("password");//密码
                int sex=rs.getInt("sex");//性别
                String identityCode=rs.getString("identityCode");//身份证号
                String email=rs.getString("email");//邮箱
                String mobile=rs.getString("mobile");//手机号
                int type=rs.getInt("type");//类型（前后台）
                User use=new User(id,loginName,userName,password,sex,identityCode,email,mobile,type);
                users.add(use);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return users;
    }
}
